package ru.evga.billboard.document;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public abstract class BytesDocPart
    extends DocPart
{
    public static int DEF_BUF_SIZE = 1024;
    private byte [] m_bytes;

    public BytesDocPart(IDocPart parentDocPart)
    {
        super(parentDocPart);
    }

    public void read(InputStream inputStream)
        throws IOException
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte [] buffer = new byte[DEF_BUF_SIZE];
        int read = 0;
        while ((read = inputStream.read(buffer, 0, buffer.length)) >= 0)
        {
            bos.write(buffer, 0, read);
        }
        m_bytes = bos.toByteArray();
    }

    public void write(OutputStream outputStream)
        throws IOException
    {
        outputStream.write(m_bytes);
    }

    public byte [] getBytes()
    {
        return m_bytes;
    }

    public void setBytes(byte [] bytes)
    {
        m_bytes = bytes;
    }
}
